import axios from 'axios';
import Router from '../router'
axios.defaults.timeout = 5000;
import qs from 'qs'
// axios.defaults.baseURL = '/api'; //填写域名
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
axios.defaults.baseURL = process.env.VUE_APP_SERVER_URL //测试
    // axios.defaults.baseURL = 'https://entapi.chinazcah.cn' //正式
    //返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
    res => {
         // 非登录请求, 请求头带上token
        return res
    },
    error => {
        if (error) {
            if (error && error.response) {
                switch (error.response.status) {
                    case 400:
                        console.log('错误请求')
                        break;
                    case 401:
                        console.log('未授权，请重新登录')
                        break;
                    case 403:
                        console.log('拒绝访问')
                        break;
                    case 404:
                        console.log('请求错误,未找到该资源')
                        break;
                    case 405:
                        console.log('请求方法未允许')
                        break;
                    case 408:
                        console.log('请求超时')
                        break;
                    case 500:
                        console.log('服务器端出错')
                        console.log(4444);
                        break;
                    case 501:
                        console.log('网络未实现')
                        break;
                    case 502:
                        console.log('网络错误')
                        break;
                    case 503:
                        console.log('服务不可用')
                        break;
                    case 504:
                        console.log('网络超时')
                        break;
                    case 505:
                        console.log('http版本不支持该请求')
                        break;
                    case 1000:
                        alert('token失效')
                    default:
                        console.log(`连接错误${err.response.status}`)
                        console.log(9999);
                }
            } else {
                console.log('连接到服务器失败')
            }
            return Promise.resolve(err.response)
        }
        // return Promise.reject(error);
    }

)
axios.interceptors.request.use(
    config => {
        if (config.url.indexOf('/sys/login') === -1) {
            config.headers.token = sessionStorage.getItem('token');
        }
        const type = config.method
        const arrayFormat = config.headers.arrayFormat || 'indices'
        console.log(arrayFormat,config.data,"----------log")
        if (type === 'post' && config.headers['Content-Type'] === 'application/x-www-form-urlencoded; charset=utf-8') {
            // post请求参数处理
            config.data = qs.stringify(config.data, { allowDots: true, arrayFormat: arrayFormat })
            console.log(config.data,"----------------")
         }
        
        return config;
    },
    error => {
        console.log('main,失败')
        return Promise.reject(error);
    }
);

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
                params: params
            })
            .then(response => {
                resolve(response);
            })
            .catch(err => {
                reject(err)
            })
    })
}
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
            })
    })
}
/**
 * 下面是获取数据的接口
 */
/**
 * 测试接口
 * 名称：exam
 * 参数：paramObj/null
 * 方式：get/post/patch/put
 */
export const api = {
    //组织架构
    UserList:(params)=>{
        return post('/sys/contact/user/list',params);
    },
    // 通过id获取资源
    getFile:(params)=>{
        return get('/sys/accessory/download',params);
    },
    //创建会议
    meetingStart:(params)=>{
        return post('/redwood/meeting/start',params);
    },
    // 创建多人会议
    meetingStartMany:(params)=>{
        return post('/redwood/meeting/start/many',params);
    },
    // 加入会议
    meetingJoin:(params)=>{
        return post('/redwood/meeting/join',params);
    },
    officeList:(params)=>{
        return post('/sys/contact/list',params);
    },
    // 最近会话
    recentSession:(params)=>{
        return post('/imMessage/info/list/mine',params);
    },
  
    // // 更具班组id查询图片
    // GetPictureInfoByDepartmentArray:(params)=>{
    //     return get('/api/RealTimeMonitor/GetPictureInfoByDepartmentArray',params);
    // },
    // //主动拍照
    // Photos:(params)=>{
    //     return post('/api/Alarm/SaveCapturePic',params);
    // },



}